package com.coltsmedia.RichmondHighNews.adapter;

import android.app.Activity;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coltsmedia.RichmondHighNews.R;
import com.coltsmedia.RichmondHighNews.rss.domain.Article;
import com.coltsmedia.RichmondHighNews.util.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ArticleListAdapter extends ArrayAdapter<Article> {

	public ArticleListAdapter(Activity activity, List<Article> articles) {
		super(activity, 0, articles);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();
		LayoutInflater inflater = activity.getLayoutInflater();

		View rowView = inflater.inflate(R.layout.fragment_article_list, null);

		Article article = getItem(position);

        TextView textView = (TextView) rowView.findViewById(R.id.card_title);
        textView.setText(article.getTitle());
        TextView dateView = (TextView) rowView.findViewById(R.id.card_description);

        ImageView imgView = (ImageView) rowView.findViewById(R.id.card_stripe);

        if (article.getAuthor().equals("Alvaro Ibarguen")){
            imgView.setBackgroundResource(R.drawable.coltsmediaadmin);
        }
        else imgView.setBackgroundResource(R.drawable.avatar_placeholder);


        String pubDate = article.getPubDate();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
        Date pDate;
        try {
            pDate = df.parse(pubDate);
            pubDate = "published " + DateUtils.getDateDifference(pDate) + " by " + article.getAuthor();
        } catch (ParseException e) {
            Log.e("DATE PARSING", "Error parsing date..");
            pubDate = "published by " + article.getAuthor();
        }


        if (article.getTitle().equals("Bell Schedule")){
            pubDate = "Click to see the Bell Schedule for the day";
        }

        dateView.setText(pubDate);

        if (!article.isRead()){
            textView.setTypeface(Typeface.DEFAULT);
        }

		return rowView;
	}
}